import axios from 'axios';
import qs from 'qs';

import path from '@/config/path';
import { rtArray } from '@/util/tools';

// default user state
const dstate = {
    userInfo: {
        id: '',
        username: '',
        userType: '',
        detail: undefined
    },
    isLogin: undefined,
    redirectTo: undefined,
    userList: []
};

// default action
const daction = {
    type: '',
    preload: {}
};

// types
const GETUSERINFO = 'getuserinfo';
const REGISTER = 'register';
const USERSTATUS = 'USERSTATUS';
const LOGIN = 'login';
const FINISHUSERINFO = 'finishuserinfo';
const CLEARLOGIN = 'clearlogin';
const GETUSERLIST = 'getuserlist';

// actions
// getUserInfo
const getUserInfoToState = (preload = {}) => {
    return {
        type: GETUSERINFO,
        preload: preload
    }
};
const getUserInfo = data => dispatch => {
    return axios.get('/api/userInfo').then((res) => {
        res = res.data;
        dispatch(getUserInfoToState(res.info));
    });
};

// register
const register = data => dispatch => {
    axios.post('/api/register', qs.stringify(data)).then(
        (res) => {
            res = res.data;
            dispatch(registerToState(res.info ? res.info : {}));
        }
    )
};
const registerToState = (preload = {}) => {
    return {
        type: REGISTER,
        preload: preload
    }
};

// user wheather login
const userStatusToState = (preload = false) => {
    return {
        type: USERSTATUS,
        preload: preload
    }
}
const userStaus = data => dispatch => {
    axios.get('/api/isLogin').then((res) => {
        res = res.data;
        dispatch(userStatusToState(res.code === 200));
    })
}

// user login
const userLoginToState = (preload = {}) => {
    return {
        type: LOGIN,
        preload: preload
    }
};
const userLogin = data => dispatch => {
    axios.post('/api/login', qs.stringify(data)).then((res) => {
        res = res.data;
        dispatch(userLoginToState(res.info));
    });
};

// finish user info
const finishUserInfoToState = (preload = {}) => {
    return {
        type: FINISHUSERINFO,
        preload: preload
    }
};
const finishUserInfo = data => dispatch => {
    axios.post('/api/finish', qs.stringify(data)).then(res => {
        res = res.data;
        if(200 === res.code){
            dispatch(finishUserInfoToState(res.info));
        }
    })
};

// clearLogin
const clearLogin = () => {
    return {
        type: CLEARLOGIN,
        preload: {}
    }
};

// get user list
const getUserListToState = (preload) => {
    return {
        type: GETUSERLIST,
        preload: preload
    }
};
const getUserList = (user = {}) => dispatch => {
    axios.get('/api/getUserList?'+qs.stringify(user)).then((res) => {
        res = res.data;
        dispatch(getUserListToState(rtArray(res.info)));
    });
};

// reducer
const userInfoTb = (state = dstate, action = daction) => {
    let res = null;

    switch(action.type){
        case GETUSERINFO:
        case LOGIN:
        case REGISTER:
            res = {...state, userInfo: action.preload, isLogin: !!action.preload.id };
            break;
        case USERSTATUS:
            res = {...state, isLogin: action.preload};
            break;
        case CLEARLOGIN:
            res = {...dstate, isLogin: !!action.preload.id};
            break;
        case FINISHUSERINFO:
            res = {...state, redirectTo: path['30002'], userInfo: {...state.userInfo, detail: action.preload}};
            break;
        case GETUSERLIST:
            res = {...state, userList: action.preload};
            break;
        default:res = state;
    }

    return res;
};

export { register, userInfoTb, userStaus, userLogin, getUserInfo, finishUserInfo, clearLogin, getUserList };